package com.wh.wisdomsite.xbox.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2014/7/9.
 */
public class VersionUtil extends PropertyPlaceholderConfigurer{

    /**
     * 版本参数
     */
    private static Map<String, String> ctxPropertiesMap;

    //参数名-版本号
    private static String VERCODE = "vercode";
    //参数名-地址
    private static String VERURL = "url";
    //参数名-版本名称
    private static String VERNAME = "vername";


    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,
                                     Properties props)throws BeansException {

        super.processProperties(beanFactory, props);
        //load properties to ctxPropertiesMap
        ctxPropertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
    }

    //static method for accessing context properties
    public static String getProperty(String name) {
        return ctxPropertiesMap.get(name);
    }

    /**
     * 返回版本参数
     * @param clientType  客户端类型
     * @return
     */
    public static Map<String, Object> getProperties(String clientType){
        String type = clientType.toLowerCase();
        Map<String, Object> prop = new HashMap<String,Object>();

        String prop_vercode = getProperty(type+"_"+VERCODE);
        prop.put(VERCODE,prop_vercode);
        String prop_vername = getProperty(type+"_"+VERNAME);
        prop.put(VERNAME,prop_vername);
        String prop_url = getProperty(type+"_"+VERURL);
        prop.put(VERURL,prop_url);

        return prop;
    }

}
